package y2015.day_15.输入输出流02;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Arrays;

/**
 * 字节流
 */
public class FileByte {
    @Test
    public void main() throws Exception {
//		input();
        output();
    }

    private static void input() {
        FileInputStream fis = null;
        try {
            File file = new File("C:\\Documents and Settings\\tarena\\桌面", "ping.txt");
            if (file.exists()) {//如果文件存在
                byte[] readByte = new byte[(int) file.length()];//设数组长度为文件长度
                fis = new FileInputStream(file);// 为file文件创建字节流输入流实例
//			fis.read(readByte);
                fis.read(readByte, 0, readByte.length);
                System.out.println(Arrays.toString(readByte));
                int b = -1;
                while ((b = fis.read()) != -1) {
                    System.out.println((char) b);

                }
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    private static void output() {
        String mytxt = "防火防盗防";
        FileOutputStream fis = null;
        File file = new File("C:\\Documents and Settings\\tarena\\桌面", "ping.txt");
        if (file.exists()) {//如果文件存在
            try {
                fis = new FileOutputStream(file);
                byte mytx[] = mytxt.getBytes();
                try {
                    fis.write(mytx, 0, 2);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
